﻿ YUI.add('editar-persona-view', function (Y) {
    'use strict';
    Y.EditarPersona = Y.Base.create('EditarPersona', Y.View, [], {
        templateIsCharged: false,
        loadTemplate: function () {
            if (this.template == '') {
                var that = this,
                validator = null,
                request = Y.io('/ExcellentProject/scripts/views/seguridades/personas/templates/editarPersona.html', {
                    on: {
                        complete: function (id, response) {
                            that.templateIsCharged = true;
                            that.template = response.responseText;
                            that.render();
                        }
                    },
                    failure: function (id, response) {
                        alert(response)
                    }
                })
            }
        },
        events: {
            '.guardar': {
                click: 'guardarPersona'
            }
        },

        render: function () {
            this.loadTemplate();            
        },

        guardarPersona: function (ev) {
            var model = this.get('model');
            model.setAttrs({
                Nombre: Y.one('#nombre').get('value'),
                Apellido: Y.one('#apellido').get('value'),
                Direccion: Y.one('#direccion').get('value'),
                Telefono: Y.one('#telefono').get('value'),
                Correo: Y.one('#correo').get('value'),
                Id: id
            });
            var options = null;
            if (model.get('Id') != 0)
                options = { action: 'update', idPers: model.get('Id') };
            else
                options = { urlName: 'saveOption' };
            var that = this, a = model.save(options, function (err, response) {
                if (err != null)
                    alert('Ocurrieron problemas al actualizar la información de la persona seleccionada')
                else {
                    bootbox.dialog({
                        message: "<h5>Persona creada</h5>",
                        title: "Confirmación",
                        show: true,
                        backdrop: true,
                        closeButton: true,
                        animate: true,
                        className: "my-modal",
                        buttons: {
                            success: {
                                label: "Ok",
                                className: "btn btn-success",
                                callback: function () { }
                            }
                        }
                    });

                    Y.one('#navigatePersona').simulate("click");

                }
            });

            setTimeout(null, 2000);
        }
    })

}, '0.0.0.1', { requires: ['view', 'handlebars', 'node', 'event', 'node-event-simulate'] })
